package graph.editor;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Point;
import java.awt.geom.Point2D;
import java.awt.geom.RectangularShape;

class Vertex {
	RectangularShape shape;
	String label;
	private static final Point2D DELTA_LABEL = new Point(1, -1);

	Vertex(RectangularShape rs, String label) {
		this.shape = rs;
		this.label = label;
	}

	public boolean contains(int x, int y) {
		return shape.contains(x, y);
	}

	void draw(Graphics2D g2) {
		g2.fill(shape);
		if (label != null)
			g2.drawString(label, (int) (shape.getMaxX() + DELTA_LABEL.getX()),
					(int) (shape.getMinY() + DELTA_LABEL.getY()));
	}
	
	/*
	 * Add new draw method to draw an Automaton state
	 */
	void drawState(Graphics2D g2) {
		Paint bg = g2.getPaint();
		g2.fill(shape);
		if (label != null) {
			g2.setFont(new Font(null, Font.BOLD, 20));
			// get metrics from the graphics
		    FontMetrics metrics = g2.getFontMetrics();
			// get the height of a line of text in this font
		    int hgt = metrics.getHeight();
		    // get the advance of my text in this font
		    int adv = metrics.stringWidth(label);
		    // draw label
		    g2.setColor(Color.WHITE);
			g2.drawString(label, (int) (shape.getCenterX() - adv/2), (int) (shape.getCenterY() + hgt/2 - 5));
		}
		g2.setPaint(bg);
	}
}
